package MyCountDownLatch;

import java.util.concurrent.CountDownLatch;

public class CountDownLatchExample {
    public static void main(String[] args) {
        int threadCount = 3;
        CountDownLatch latch = new CountDownLatch(threadCount);

        for (int i = 0; i < threadCount; i++) {
            Thread thread = new Thread(() -> {
                System.out.println("Thread " + Thread.currentThread().getName() + " is running");
                latch.countDown(); // 当前线程完成任务，计数器减一
            });
            thread.start();
        }

        try {
            latch.await(); // 主线程等待计数器变为0
            System.out.println("All threads have completed their tasks. Main thread resumes execution.");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

